package com.chen.shopping_category_service.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.shopping_category_service.mapper.CategoryMapper;
import com.chen.shopping_common.pojo.Category;
import com.chen.shopping_common.service.CategoryService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Arrays;
import java.util.List;

@DubboService
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void add(Category category) {
        categoryMapper.insert(category);
        refreshRedisCategory();
    }

    @Override
    public void update(Category category) {
        categoryMapper.updateById(category);
        refreshRedisCategory();
    }

    @Override
    public void delete(Long[] ids) {
        categoryMapper.deleteBatchIds(Arrays.asList(ids));
        refreshRedisCategory();
    }

    @Override
    public Category findById(Long id) {
        Category category = categoryMapper.selectById(id);
        return category;
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Category category = categoryMapper.selectById(id);
        category.setStatus(status);
        categoryMapper.updateById(category);
    }

    @Override
    public Page<Category> findByPage(int page,int size) {
        Page<Category> categoryPage = categoryMapper.selectPage(new Page<>(page, size), null);
        return categoryPage;
    }

    //查询所有启动广告
    @Override
    public List<Category> findAll() {
        //先从redis查询
        //获取redis中操作list的对象
        ListOperations<String,Category> listOperations = redisTemplate.opsForList();
        //获取数据
        List<Category> categoriesList = listOperations.range("categories", 0, -1);
        if (categoriesList != null && categoriesList.size() > 0){
            System.out.println("从Redis中查询");
            return categoriesList;
        }else {
            //查询没有再到数据库查询
            System.out.println("从数据库中查询");
            //从数据库查询所有启用广告
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("status",1);
            List<Category> categories = categoryMapper.selectList(queryWrapper);
            listOperations.leftPushAll("categories", categories);
            return categories;
        }
    }

    public void refreshRedisCategory(){
        //从数据库中查询广告
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("status",1);
        List<Category> categories = categoryMapper.selectList(queryWrapper);
        //从redis中删除旧广告数据
        redisTemplate.delete("categories");
        //向redis中添加新的广告数据
        //获取redis中操作list的对象
        ListOperations<String,Category> listOperations = redisTemplate.opsForList();
        listOperations.leftPushAll("categories",categories);
    }
}
